package com.beiming.libra.controller;

import com.beiming.libra.common.Assert;
import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.domain.dto.requestdto.caseapply.CaseApplyRequestDTO;
import com.beiming.libra.domain.dto.requestdto.caseapply.DisputeRequestDTO;
import com.beiming.libra.domain.dto.requestdto.caseapply.MultiAddCaseRequestDTO;
import com.beiming.libra.domain.dto.responsedto.caseapply.MutiAddCaseResponseDTO;
import com.beiming.libra.helper.ContextUtil;
import com.beiming.libra.helper.Result;
import com.beiming.libra.service.CaseApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 案件审理controller
 *
 * @author tp Date : 2018/6/4/004 18:08
 */
@RestController
@RequestMapping("/api/caseApply")
@Api(value = "案件书面审理申请", tags = "案件书面审理申请")
public class CaseApplyController {

  @Resource
  private CaseApplyService caseApplyService;

  private Logger logger = LoggerFactory.getLogger(CaseApplyController.class);

  /**
   * 获得纠纷类型
   */
  @GetMapping("disputeType/{type}")
  @ApiOperation(value = "获得纠纷类型", notes = "获得纠纷类型 1、金融类，2、非金融类")
  public Result<List<DisputeRequestDTO>> disputeType(
      @ApiParam("案件类型") @PathVariable("type") Integer type) {
    Assert.isNotNull(type, ErrorCode.ILLEGAL_PARAMETER);
    return Result.success(this.caseApplyService.selectDisputeType(type));
  }

  /**
   * 验证用户是否实名
   */
  @GetMapping("authentication")
  @ApiOperation(value = "认证用户是否实名", notes = "认证用户是否实名返回布尔值,true为已经认证")
  public Result<Boolean> authentication() {
    return Result.success(this.caseApplyService.authentication(ContextUtil.getCurrentUserId()));
  }

  /**
   * 新增案件
   *
   * @return result
   */
  @PostMapping("")
  @ApiOperation(value = "案件新增接口", notes = "案件新增接口")
  public Result addCase(@RequestBody @Valid CaseApplyRequestDTO caseApplyRequestDTO) {
    return Result.success(this.caseApplyService
        .insertArbitrationCase(caseApplyRequestDTO, ContextUtil.getCurrentUserId()));
  }

  /**
   * 批量导入案件
   */
  @PostMapping("multiAddCase")
  @ApiOperation(value = "批量导入案件", notes = "批量导入案件")
  public Result<List<MutiAddCaseResponseDTO>> multiAddCase(
      @Valid MultiAddCaseRequestDTO multiAddCaseRequestDTO, HttpServletRequest request) {
    try {
      List<MutiAddCaseResponseDTO> mutiAddCaseResponseDTOS = this.caseApplyService
          .multiAddCase(multiAddCaseRequestDTO, request);
      if (!CollectionUtils.isEmpty(mutiAddCaseResponseDTOS)) {
        return Result.failed(ErrorCode.EXCEL_INVALIDATED, "excel解析出错", mutiAddCaseResponseDTOS);
      }
      return Result.success();
    } catch (Exception e) {
      logger.error("异常：{}", e.getMessage());
      e.printStackTrace();
      return Result.failed(ErrorCode.UNEXCEPTED, "系统错误");
    }

  }

  /**
   * 获得证据类型
   */
  @GetMapping("evidenceType")
  @ApiOperation(value = "上传证据获取证据类型以及证明对象", notes = "（evidenceType：证据类型）,code 对应 select中value,name对应text")
  public Result<Map<String, Object>> getEvidenceType() {
    return Result.success(this.caseApplyService.getEvidenceType());
  }

}
